package fy.form.hr_plans.manager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import fy.form.hr_plans.bean.PlanBean;
import fy.form.hr_plans.conf.Hr_PlansConf;
import fy.form.hr_plans.dao.PlansDao;
import fy.form.hr_plans.operatorImpl.PersonalDevelopmentPlansOperatorImpl;
import fy.form.listen.bean.CommonFlowBean;
import fy.form.listen.dao.MyCommonFlowDao;
import fy.msg.MessageSender;

public class PlansManager {

	private static Log log = LogFactory.getLog(PlansManager.class);

	private long summaryId;

	private long userID;

	private String flowTitle;

	private long creator;

	public boolean isPersonalDevelopmentPlans(long summaryId) {
		CommonFlowBean bean = MyCommonFlowDao
				.getTempleteCodeBySummaryId(summaryId);
		if (bean != null
				&& Hr_PlansConf.checkTempletCode(bean.getTempleteCode())) {
			this.summaryId = summaryId;
			this.userID = bean.getUserID();
			return true;
		}
		return false;
	}

	// public boolean isPersonalDevelopmentPlans(String code, long summaryId)
	// throws Exception {
	// if (Hr_PlansConf.checkTempletCode(code)) {
	// this.summaryId = summaryId;
	// this.userID = MyCommonFlowDao.getSenderBySummaryId(summaryId);
	// return true;
	// }
	// return false;
	// }

	/**
	 * 
	 * @param code
	 * @param summaryId
	 * @param userID
	 * @return
	 */
	public boolean isPersonalDevelopmentPlans(long summaryId, long userID) {
		CommonFlowBean bean = MyCommonFlowDao
				.getTempleteCodeBySummaryId(summaryId);
		if (bean != null
				&& Hr_PlansConf.checkTempletCode(bean.getTempleteCode())) {
			this.summaryId = summaryId;
			this.userID = userID;
			return true;
		}
		return false;
	}

	public void save(short state, String operate) {
		boolean success = false;
		if (state == Hr_PlansConf.STATE_FINISH) {
			success = PlansDao.finishPlan(summaryId);
		} else if (state == Hr_PlansConf.STATE_CANCER
				|| state == Hr_PlansConf.STATE_STOP) {
			success = PlansDao.invalidPlan(summaryId);
		} else if (state == Hr_PlansConf.STATE_TAKEBACK
				|| state == Hr_PlansConf.STATE_STEPBACK) {
			success = PlansDao.flowBackPlan(summaryId);
		} else if (state == Hr_PlansConf.STATE_START) {
			PlanBean bean = parseFormValues(summaryId);
			success = PlansDao.newPlan(bean);
		} else if (state == Hr_PlansConf.STATE_PROCESS) {
			PlanBean bean = parseFormValues(summaryId);
			success = PlansDao.updatePlan(bean);
		}
		if (!success) {
			log.error(new StringBuilder().append(operate).append("summaryID:")
					.append(summaryId).append("存储数据失败"));
			MessageSender.sendMsg(creator, new StringBuilder().append("表单《")
					.append(flowTitle).append("》写入数据库发生异常，请联系人事部，手工录入")
					.toString());
		}
	}

	private PlanBean parseFormValues(long summaryId) {
		try {

			PersonalDevelopmentPlansOperatorImpl pdpo = new PersonalDevelopmentPlansOperatorImpl();

			pdpo.operate(summaryId, userID);

			flowTitle = pdpo.getFormBean().getFlowExport().getFlowTitle();
			creator = pdpo.getFormBean().getFlowExport().getFlowCreatePerson()
					.getId();

			pdpo.parse();
			return pdpo.getPlanBean();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
